package com.longcai.service.impl.customer;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.longcai.bean.ApiResult;
import com.longcai.entity.customer.ClyjUserCard;
import com.longcai.mapper.customer.ClyjUserCardMapper;
import com.longcai.service.customer.ClyjUserCardService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 * 用户银行卡表 服务实现类
 * </p>
 *
 * @author M
 * @since 2019-04-18
 */
@Service
public class ClyjUserCardServiceImpl extends ServiceImpl<ClyjUserCardMapper, ClyjUserCard> implements ClyjUserCardService {
    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private ClyjUserCardMapper clyjUserCardMapper;

    /**
     * 用户绑定银行卡
     *
     * @param userId
     * @param params
     * @return
     */
    @Override
    public ApiResult saveCard(String userId, JSONObject params, String cardNo) {
        //用户姓名
        String name = params.getString("name");
        //银行名称
        String bank = params.getString("bank");
        //银行预留手机号
        String phone = params.getString("phone");

        try {
            ClyjUserCard clyjUserCard = new ClyjUserCard();
            clyjUserCard.setId(UUID.randomUUID().toString().replace("-", ""));
            clyjUserCard.setBank(bank);
            clyjUserCard.setCardNo(cardNo);
            clyjUserCard.setCreateTime(new Date());
            clyjUserCard.setPhone(phone);
            clyjUserCard.setUserId(userId);
            clyjUserCard.setUserName(name);
            clyjUserCard.setStatus(0);
            this.clyjUserCardMapper.insert(clyjUserCard);
            logger.info("绑定成功");
            return ApiResult.success("绑定成功");
        } catch (Exception e) {
            logger.error("绑定失败");
            e.printStackTrace();
            return ApiResult.fail("绑定失败"+e.getMessage());
        }
    }

    /**
     * 获取银行卡列表
     *
     * @return
     */
    @Override
    public ApiResult getCardList(String userId) {
        try {
            QueryWrapper<ClyjUserCard> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("user_id", userId);
            List<ClyjUserCard> clyjUserCardList = this.clyjUserCardMapper.selectList(queryWrapper);
            logger.debug("查询成功");
            return ApiResult.success(clyjUserCardList);
        } catch (Exception e) {
            logger.error("查询失败");
            e.printStackTrace();
            return ApiResult.fail("查询失败"+e.getMessage());
        }
    }


}
